//
//@File    :	14.17_arrytp.h
//@Time    :	2022/02/24 09:14:00
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	Array Template
//

#ifndef ARRYTP_H_
#define ARRYTP_H_

#include <iostream>
#include <cstdlib> 
//每种数组大小都将生成自己的模板
//ArrayTP<double,12> egweights;
//改进,将数组成员作为类成员
//ArrayTP<int> eggs(12)//推荐使用!
template <typename T,int n>
class ArrayTP
{
private:
    T ar[n];
public:
    ArrayTP(){};
    explicit ArrayTP(const T &v);
    virtual T &operator[](int i);
    virtual T operator[](int i)const;
};

template<typename T,int n>
ArrayTP<T,n>::ArrayTP(const T & v)
{
    for (int i = 0; i < n; i++)
    {
        ar[i]=v;
    }
}
template<typename T,int n>
T &ArrayTP<T,n>::operator[](int i)
{
    if (i<0||i>=n)
    {
        std::cerr<<"Error in array limits: "<<i
        <<" is out of range\n";
        std::exit(EXIT_FAILURE);
    }
    return ar[i];
}

template<typename T,int n>
T ArrayTP<T,n>::operator[](int i)const{
    if (i<0||i>=n)
    {
        std::cerr<<"Error in array limits: "<<i
        <<" is out of range\n";
        std::exit(EXIT_FAILURE);
    }
    return ar[i];
}
#endif	// ARRYTP_H_